<?php

/*

___Canvas Plugin Form___________________________________________

Outputs the Lightbox form for plugins, building individual
form elements based upon the plugin definitions.

________________________________________________________________

*/

require('../../../wp-config.php');

$zone = $_GET['zone'];
$position = $_GET['position'];
$block_id = $_GET['block_id'];
$counter = 0;
?>
<div class="lbContent">
<?php
	global $wpdb, $canvas;
	$variables = $wpdb->get_results("SELECT * FROM ".$canvas->variables." WHERE parent = '$block_id' ORDER BY variable_id");
	$title = $wpdb->get_results("SELECT * FROM ".$canvas->main." WHERE block_id = '$block_id'");
	$content = '<h3>'.$title[0]->block_name.'';
	if($title[0]->author != '' && $title[0]->uri != '')
		$content .= '<span>by <a target="new" href="'.$title[0]->uri.'">'.$title[0]->author.'</a></span>';
	if($title[0]->description != '')
		$content .= '<p>'.$title[0]->description.'</p>';
	$content .= '<a id="confirm" href="javascript:void(0)" class="lbAction" rel="deactivate">Save and Return</a>';
	$content .= '<a id="cancel" href="javascript:void(0)" class="lbAction" rel="cancel">Cancel</a></h3><div>';
    $content .= "\n".'<form id="lightbox_form">';
	$content .= '<input type="hidden" name="parent" value="'.$block_id.'">'."\n";

	foreach($variables as $variable) {
		if(strtolower($variable->type) == 'text')
			$content .= '<label class="textbox">'.$variable->description.': <input type="text" class="text" name="'.$variable->variable_name.'" value="'.$variable->value.'" /></label>'."\n";
		elseif(strtolower($variable->type) == 'textarea')
			$content .= '<label class="textbox">'.$variable->description.': <textarea rows="8" name="'.$variable->variable_name.'">'.htmlspecialchars($variable->value).'</textarea></label>'."\n";
		elseif(strtolower($variable->type) == 'integer')
			$content .= '<label class="textbox">'.$variable->description.': <input type="text" size="3" name="'.$variable->variable_name.'" value="'.$variable->value.'" /></label>'."\n";
		elseif(strtolower($variable->type) == 'boolean') {
			$content .= '<label class="boolean"><input type="checkbox" name="'.$variable->variable_name.'" value="1" ';
			if($variable->value == 1) $content .= 'checked ';
			$content .= '/>&nbsp;'.$variable->description.'</label>'."\n";
		} elseif(strtolower($variable->type) == 'radio') {
			$content .= '<span><p>'.$variable->description.':</p>';
			
			$options = $wpdb->get_results("SELECT * FROM ".$canvas->options." WHERE var_id = '$variable->variable_id'");
			
			foreach($options as $option) {
				// If is a Wordpress database variable...
				if(preg_match('/(.*)\-\>(.*)\:(.*)/', $option->option_value, $matches)) {
					$content .= canvas_database_plugin($matches, 'radio', $variable->value);
				} else {
					$content .= '<label><input type="radio" name="'.$variable->variable_name.'" value="'.$option->option_value.'" ';
					if ($option->option_value == $variable->value) $content .= ' checked="checked" ';
					$content .= '/>&nbsp;'.$option->option_text.'</label>';
				}
			}
			$content .= '</span>'."\n";
		} elseif(strtolower($variable->type) == 'select') {
			$content .= '<span><p>'.$variable->description.':</p>';

			$options = $wpdb->get_results("SELECT * FROM ".$canvas->options." WHERE var_id = '$variable->variable_id'");

			$content .= '<select name="'.$variable->variable_name.'" id="'.$variable->variable_name.'">'."\n";
			foreach($options as $option) {
				// If is a Wordpress database variable...
				if(preg_match('/(.*)\-\>(.*)\:(.*)/', $option->option_value, $matches)) {
					$content .= canvas_database_plugin($matches, 'select', $variable->value);
				} else {
					$content .= '<option value="'.$option->option_value.'" ';
					if ($option->option_value == $variable->value) $content .= ' selected="selected" ';
					$content .= '>&nbsp;'.$option->option_text."</option>\n";
				}
			}
			$content .= '</select>'."\n";
			$content .= '</span>'."\n";
        } elseif(strtolower($variable->type) == 'image') {
			$options = $wpdb->get_results("SELECT * FROM ".$canvas->options." WHERE var_id = '$variable->variable_id'");
			$content .= '<p>'.$variable->description.':</p>';
			$content .= '<input type="hidden" id="directory" value="'.$options[0]->option_value.'">';
			$selected = '<input type="hidden" id="selected_image">';
			$content .= '<input type="hidden" id="path" name="'.$variable->variable_name.'" value="'.$variable->value.'">';
			$content .= '<div class="gallery">';
			$files = scandirectory(ABSPATH.$options[0]->option_value);
			foreach($files as $file) {
				$class = '';
				if(substr($file, -3) == 'jpg' || substr($file, -3) == 'gif' ||  substr($file, -3) == 'png') {
					if($options[0]->option_value.$file == $variable->value) {
						$class= 'class="selected_image"';
						$selected = '<input type="hidden" id="selected_image" value="'.$file.'">';
					}
					list($width, $height) = @getimagesize(get_bloginfo('wpurl').'/'.$options[0]->option_value.$file);
					if($width > 250) { $height = $height * 250 / $width; $width = 250; }
					$content .= '<a href="javascript:void(0)" onclick="Canvas.gallerySwitch(\''.$file.'\')"><img '.$class.' id="'.$file.'" src="'.get_bloginfo('wpurl').'/'.$options[0]->option_value.$file.'" title="'.$file.'" alt="'.$file.'" style="height: '.$height.'px; width: '.$width.'px;" /></a>';
				}
			}
			$content .= $selected;
			$content .= '</div>';
        } elseif(strtolower($variable->type) == 'list') {
        	$listitems = explode('|', $variable->value);
        	$content .= '<label class="textbox">'.$variable->description.' <small>(Note &#124; is an illegal character)</small></label>'."\n".'<span class="list" id="listlist">'."\n";
        	foreach($listitems as $listitem) {
        		$content .= '<span id="'.$variable->variable_name.$counter.'"><input type="text" class="text" name="'.$counter.'canvaslist_'.$variable->variable_name.'" value="'.htmlspecialchars($listitem).'" />'."\n";
				$content .= '<a href="javascript:void(0)" onclick="Canvas.addListItem(this, \''.$variable->variable_name.'\')"><img src="'.CANVASURI.'images/list-duplicate.png" alt="Duplicate" title="Duplicate item"/></a>';
				$content .= '<a href="javascript:void(0)" onclick="Canvas.removeListItem(this)"><img src="'.CANVASURI.'images/list-delete.png" alt="Delete" title="Delete item"/></a>';
				$content .= '</span>';
				$counter++;
			}
        	$content .= '</span>'."\n";
        } elseif(strtolower($variable->type) == 'gmodule') {
        	//$variable->value WILL THIS BE THE XML FILE?
        	$xmlfile = 'http://www.google.com/ig/modules/datetime.xml';
        	$url = 'http://gmodules.com/ig/creator?synd=open&url='.$xmlfile;
        	echo '<object id="gmodule_content" data="'.$url.'" type="text/html"></object>';
        } elseif(strtolower($variable->type) == 'gmodule_script') {
        	echo '<input type="text" id="gmodule_script" value="'.$variable->value.'">';
        }
	}
	if(count($variables) == 0) echo 'This plugin has no options (yes, it should be a block instead).';	

	$content .= '<input type="hidden" id="listcount" name="listcount" value="'.$counter.'">'."\n";
	$content .= '</form>'."\n";
		
	echo $content;

?>
</div>
</div>